
def telnetdo(HOST=None,USER=None,PASS=None,COMD=None):
	import telnetlib,sys
	if not HOST:
		try:
			HOST=sys.argv[1]
			USER=sys.argv[2]
			PASS=sys.argv[3]
			COMD=sys.argv[4]
		except:
			print "Usage: telnetdo.py host user pass command"
			return
	msg=['Debug mesages:\r\n'] 

	tn = telnetlib.Telnet()
	
	try:
		tn.open(HOST)
	except:
		print "Cannot open host:"+HOST
		return
	msg.append(tn.expect(["Username:"],5))
	tn.write(USER+"\r\n")
	
	if PASS:
		msg.append(tn.expect(["Password:"],5))
		tn.write(PASS+"\r\n")
	msg.append(tn.expect([">"],5))
	tn.write(COMD+"\r\n")
	msg.append(tn.expect([">"],5))
	tn.close()
	del tn
	return msg
	
if __name__=='__main__':
	print telnetdo()
	
# tn.read_until("name:")
# tn.write(user + "\r\n")
# if password:
    # tn.read_until("password:")
    # tn.write(password + "\r\n")
# print tn.expect([">"])
# time.sleep(1)
# tn.interact()
# print tn.read_sb_data()
# tn.write("quit\r\n")
# tn.read_until("[n]:")
# tn.write("y\r\n")
# print tn.read_all()
# tn.close()
